package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.admin.mapper.TbAdminMapper;
import com.usian.admin.service.TbAdminService;
import com.usian.model.admin.dtos.TbAdminDto;
import com.usian.model.admin.pojos.TbAdmin;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbAdminServiceImpl extends BaseServiceImpl<TbAdminMapper, TbAdmin> implements TbAdminService {

    @Autowired
    private TbAdminMapper tbAdminMapper;

    @Override
    public ResponseResult select(TbAdminDto tbAdmin) {
        if(tbAdmin == null){
            return errorParam;
        }else {
            LambdaQueryWrapper<TbAdmin> wrapper = Wrappers.<TbAdmin>lambdaQuery()
                    .like(StringUtils.isNotBlank(tbAdmin.getRealName()), TbAdmin::getRealName, tbAdmin.getRealName())
                    .like(tbAdmin.getUserState()!=null, TbAdmin::getUserState, tbAdmin.getUserState());
            ResponseResult<Object> result = new ResponseResult<>();
            List<TbAdmin> list = list(wrapper);
            result.setData(list);
            return result;
        }

    }

    @Override
    public ResponseResult uptone(TbAdmin tbAdmin) { //启用
        //参数检测
        if(tbAdmin == null){
            return errorParam;
        }else {
            List<TbAdmin> adminList = tbAdminMapper.selectList(null);
            for (TbAdmin admin : adminList) {
               if(admin.getId().equals(tbAdmin.getId())){
                   //找到当前id后更新状态为禁用
                   admin.setUserState(1);
                   //更新到数据库中
                   tbAdminMapper.updateById(admin);
               }
            }
            return  success;
        }
    }

    @Override
    public ResponseResult upttwo(TbAdmin tbAdmin) { //禁用
        //参数检测
        if(tbAdmin == null){
            return errorParam;
        }else {
            List<TbAdmin> adminList = tbAdminMapper.selectList(null);
            for (TbAdmin admin : adminList) {
                if(admin.getId().equals(tbAdmin.getId())){
                    //找到当前id后更新状态为启用
                    admin.setUserState(0);
                    //更新到数据库中
                    tbAdminMapper.updateById(admin);
                }
            }

            return  success;
        }
    }


    @Override
    public ResponseResult ist(TbAdmin tbAdmin) {
        //参数检测
        if(tbAdmin == null){
            return errorParam;
        }else {
            //更新到数据库中
            updateById(tbAdmin);
            return  success;
        }
    }
}
